import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/3 15:15
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution2 = new Solution2();
        int[] candidates = {2, 3, 6, 7};
        solution2.combinationSum(candidates, 7);
    }
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> combine = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        dfs(candidates, target, 0, 0);
        return ans;
    }
    public void dfs(int[] candidates, int target, int begin, int sum) {
        if (sum == target) {
            ans.add(new ArrayList<>(combine));
            return;
        }
        for (int i = begin; i < candidates.length; i++) {
            int temp = candidates[i] + sum;
            if (temp <= target) {
                combine.add(candidates[i]);
                dfs(candidates, target, i, temp);
                combine.remove(combine.size() - 1);
            }else {
                break;
            }
        }
    }
}
